﻿using System;
using Common;
using Dto;
using SqlServerDAL;

namespace Business
{
    public class AnnexBusiness
    {
        private const string Waming = "ChinaStone.Business.AnnexBusiness";
        private readonly AnnexDAL annexDal = new AnnexDAL();

        public AnnexDto AddAnnex(AnnexDto dto)
        {
            try
            {
                var annexId = annexDal.Save(dto);
                dto.AnnexID = annexId;
                return dto;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".AddAnnex(Annexdto dto)", ex);
                return null;
            }
        }

        public bool DeleteAnnex(int id)
        {
            if (id == 0)
            {
                return false;
            }
            try
            {
                annexDal.Delete(id);
                return true;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".DeleteAnnex(int id)", ex);
                return false;
            }
        }

        public AnnexDto GetAnnexById(int id)
        {
            if (id == 0)
            {
                return null;
            }
            try
            {
                var ht = annexDal.FindById(id);
                AnnexDto dto = null;
                if (ht != null)
                {
                    dto = FillDto.FillAnnexDto(ht);
                }
                return dto;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".GetAnnexById(int id)", ex);
                return null;
            }
        }
    }
}